-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree sort.cpp
60 lines (53 loc) · 1.15 KB
/
Tree sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;
struct NODE {
int key;
NODE *left;
NODE *right;
};
NODE *add(NODE *node, int key){
if(node == 0){
node = new NODE;
node->key = key;
return node;
} else {
if(key <= node->key){
node->left = add(node->left, key);
} else {
node->right = add(node->right, key);
}
return node;
}
}
void inorder(NODE *root, int A[], int &i){
if(root != 0){
inorder(root->left, A, i);
A[i++] = root->key;
inorder(root->right, A, i);
}
}
void treeSort(int A[], int n){
if(n > 0){
NODE *root = 0;
root = add(root, A[0]);
for(int i = 1; i < n; i++){
root = add(root, A[i]);
}
int i = 0;
inorder(root, A, i);
} else {
cout << "The array must have one or more elements" << endl;
}
}
void print(int A[], int n){
for (int i = 0; i < n; i++) {
cout << "[" << A[i] << "] ";
}
cout << endl;
}
int main(){
int A[] = {1, 10, 7, 3, 9, 8, 5, 6, 4, 2};
int n = sizeof(A)/sizeof(A[0]);
treeSort(A, n);
print(A, n);
}